package org.jypj.dev.task;

import org.jypj.dev.model.StationWeather;
import org.jypj.dev.model.TrainStation;
import org.jypj.dev.service.IStationWeatherService;
import org.jypj.dev.service.ITrainStationService;
import org.jypj.dev.util.WeatherUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenyuyuyu on 2017/5/30.
 */
@Component
public class WeatherTask {

    @Resource
    private ITrainStationService trainStationService;

    @Resource
    private IStationWeatherService stationWeatherService;

    /**
     * 自动生成表
     */
    @Scheduled(cron = "0 59 01 * * ?")
    public void autoGetWeather() {
        System.out.println("---开始生成天气数据---");
        Map<String, Object> queryMap = new HashMap<>();
        List<TrainStation> trainStations = trainStationService.selectAllStation(queryMap);
        WeatherUtil weatherUtil = new WeatherUtil();
        for (TrainStation trainStation : trainStations) {
            try {
                List<StationWeather> stationWeathers = weatherUtil.getRoadWeatherByTrainStation(trainStation);
                if (stationWeathers != null && stationWeathers.size() > 0) {
                    stationWeatherService.saveStationWeathers(stationWeathers);
                }
            } catch (Exception e) {
            }

        }
        System.out.println("---生成天气数据结束---");
    }
}
